/*
 * @Descripttion:
 * @Author: voanit
 * @Date: 2022-09-01 16:38:18
 * @LastEditors: voanit
 * @LastEditTime: 2022-09-01 18:34:23
 */
import { defineStore } from 'pinia'

const useTodoStore = defineStore('todo', {
  state: () => {
    return {
      list: [
        {
          id: 1,
          name: '吃饭',
          done: false,
        },
        {
          id: 2,
          name: '睡觉',
          done: true,
        },
        {
          id: 3,
          name: '打豆豆',
          done: false,
        },
      ],
      footList: ['All', 'Active', 'Completed'],
      activeClass: 'All',
    }
  },
  actions: {
    /* 更新状态 */
    AchangeStatus(id) {
      const objItem = this.list.find((item) => item.id == id)
      objItem.done = !objItem.done
    },

    /* 删除 */
    AchangeDel(id) {
      this.list = this.list.filter((item) => item.id !== id)
    },

    /* 添加 */
    Aadd(name) {
      this.list.push({
        id: Date.now(),
        name,
        done: false,
      })
    },

    /* 反选 */
    chkAll(chk) {
      this.list.forEach((item) => {
        item.done = chk
      })
    },

    changeClass(active) {
      this.activeClass = active
    },
  },
  getters: {
    isAll() {
      return this.list.every((item) => item.done)
    },
    unTotal() {
      return this.list.filter((item) => !item.done).length
    },

    newList() {
      switch (this.activeClass) {
        case 'All':
          return this.list
        case 'Active':
          return this.list.filter((item) => item.done)
        case 'Completed':
          return this.list.filter((item) => !item.done)
      }
    },
  },
})

export default useTodoStore
